package com.easychat.service.impl;

import com.easychat.entity.ChatSession;
import com.easychat.mappers.ChatSessionDao;
import com.easychat.service.ChatSessionService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (ChatSession)表服务实现类
 *
 * @author makejava
 * @since 2024-10-19 22:24:41
 */
@Service("chatSessionService")
public class ChatSessionServiceImpl implements ChatSessionService {
    @Resource
    private ChatSessionDao chatSessionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param sessionId 主键
     * @return 实例对象
     */
    @Override
    public ChatSession queryById(String sessionId) {
        return this.chatSessionDao.queryById(sessionId);
    }

    /**
     * 分页查询
     *
     * @param chatSession 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ChatSession> queryByPage(ChatSession chatSession, PageRequest pageRequest) {
        long total = this.chatSessionDao.count(chatSession);
        return new PageImpl<>(this.chatSessionDao.queryAllByLimit(chatSession, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param chatSession 实例对象
     * @return 实例对象
     */
    @Override
    public ChatSession insert(ChatSession chatSession) {
        this.chatSessionDao.insert(chatSession);
        return chatSession;
    }

    /**
     * 修改数据
     *
     * @param chatSession 实例对象
     * @return 实例对象
     */
    @Override
    public ChatSession update(ChatSession chatSession) {
        this.chatSessionDao.update(chatSession);
        return this.queryById(chatSession.getSessionId());
    }

    /**
     * 通过主键删除数据
     *
     * @param sessionId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String sessionId) {
        return this.chatSessionDao.deleteById(sessionId) > 0;
    }
}
